/*
 * @Author: yangzonglong
 * @Date: 2021-09-01 14:00:15
 * @version: v1.0.0
 * @Descripttion: 
 * @LastEditors: yangzonglong
 * @LastEditTime: 2021-09-01 14:27:27
 * @Auditor: 
 */
const controlTypes = ['input', 'select', 'datePicker', 'radio', 'checkbox', 'select'];

export default (form, formData) => {
  const _form = typeof form === 'string' ? JSON.parse(form) : {},
    _formData = typeof formData === 'string' ? JSON.parse(formData) : {},
    list = _form.list;

  if (!list) return {};

  const obj = {};

  list.forEach(item => {
    if(!controlTypes.includes(item.type)) return;

    const label = item.options?.formItemProps?.label;
    const key = item.options?.formItemProps?.name || item.id;
    const value = _formData[key];

    if(!value) {
      obj[label] = '';
      return;
    }

    if(['radio','select','checkbox'].includes(item.type)) {
      const dataSource = item.options?.[`${item.type}Props`]?.dataSource || [];
      obj[label] = dataSource.filter(item => value.includes(item.value)).map(item => item.label).join(',');
    }else {
      obj[label] = value;
    }
  })
  
  return obj;
}